# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: 大众案例.py
"""

'''



'''



from lxml import etree

import requests
from io import BytesIO
from fontTools.ttLib import TTFont

def request(url):
    headers = {
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Cookie": "_lxsdk_cuid=17ef3412702c8-05d063ccfd8e04-576153e-1fa400-17ef3412702c8; _lxsdk=17ef3412702c8-05d063ccfd8e04-576153e-1fa400-17ef3412702c8; _hc.v=77d0bbe0-94b9-387c-8a95-a74678d0d2f0.1644758641; s_ViewType=10; cy=344; cye=changsha; _lxsdk_s=180501c2d90-446-134-6d4%7C%7C19; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1648477843,1650611466; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1650611466",
        "Host": "www.dianping.com",
        "Pragma": "no-cache",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
    }
    res = requests.get(url, headers=headers)
    return res

# res = requests.get(url,headers=headers)
# with open('dz.html','w',encoding='utf-8') as f:
#     f.write(res.text)

def get_font():
    url1 = 'https://s3plus.meituan.net/v1/mss_73a511b8f91f43d0bdae92584ea6330b/font/a8b5e448.woff'
    font_res = request(url1)
    woff_data = BytesIO(font_res.content)  # 保存字体数据
    font = TTFont(woff_data)  # 读取woff数据
    return font

# src: url("//s3plus.meituan.net/v1/mss_73a511b8f91f43d0bdae92584ea6330b/font/a8b5e448.eot?#iefix") format("embedded-opentype"),url("//s3plus.meituan.net/v1/mss_73a511b8f91f43d0bdae92584ea6330b/font/a8b5e448.woff");
def get_data():
    font_dict = {
        'unie82e': '0',
        'unie5e3': '1',
        'unif8ae': '2',
        'uniebe1': '3',
        'unied51': '4',
        'uniea30': '5',
        'unie779': '6',
        'unif58c': '7',
        'unie44d': '8',
        'uniea4a': '9',
    }
    url = 'https://www.dianping.com/search/keyword/344/0_%E8%B6%B3%E6%B5%B4%E5%BA%97'
    res = request(url)
    res.encoding = 'UTF-8'
    text = res.text
    for i in font_dict:
        if i in text:
            text = text.replace(str(i), font_dict[i].replace(';', ''))
    selector = etree.HTML(text)
    lists = selector.xpath('//*[@id="shop-all-list"]/ul/li')
    for j in lists:
        title = j.xpath('.//div[@class="tit"]/a/h4/text()')
        st = j.xpath('.//div[@class="comment"]/a[1]/b//text()')
        f = []
        for i in st:
            f.append(i.replace(';', ''))
        print(title, ''.join(f))
get_data()



